<?php
namespace app\common\model;
use think\Model;
use think\Db;
class OrderGoods extends Model
{
    //定义表名
    protected $name = 'massage_service_order_goods_list';
    protected $append = [
        'refund_num'
    ];
    /**
     * @param $value
     * @param $data
     * @功能说明:获取退款的数量
     * @author chenniang
     * @DataTime: 2021-04-12 10:46
     */
    public function getRefundNumAttr($value,$data){
        if(!empty($data['id'])){
            $refund_model = new RefundOrder();
            $num = $refund_model->refundNum($data['id']);
            return $num;
        }
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){
        $res = $this->insert($data);
        return $res;
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){
        $res = $this->where($dis)->update($data);
        return $res;
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){
        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();
        return $data;
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataSelect($dis){
        $data = $this->where($dis)->order('id desc')->select()->toArray();
        return $data;
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){
        $data = $this->where($dis)->find();
        return !empty($data)?$data->toArray():[];
    }
    /**
     * @author chenniang
     * @DataTime: 2021-03-22 11:12
     * @功能说明:添加商品子订单
     */
    public function orderGoodsAdd($order_goods,$order_id,$cap_id,$user_id){
        $goods_model = new Service();
        $car_model   = new Car();
        foreach ($order_goods as $v){
            $ser_status = $goods_model->where(['id'=>$v['service_id']])->value('status');
            if($ser_status!=1){
                return ['code'=>500,'msg'=>'服务已经下架'];
            }
            $insert = [
                'uniacid'        => $v['uniacid'],
                'order_id'       => $order_id,
                'user_id'        => $user_id,
                'pay_type'       => 1,
                'coach_id'       => $cap_id,
                'goods_name'     => $v['title'],
                'goods_cover'    => $v['cover'],
                'price'          => $v['price'],
                'true_price'     => round($v['true_price']/$v['num'],5),
                'num'            => $v['num'],
                'can_refund_num' => $v['num'],
                'goods_id'       => $v['service_id'],
                'time_long'      => $v['time_long']
            ];
            $res = $this->dataAdd($insert);
            if($res!=1){
                return ['code'=>500,'msg'=>'下单失败'];
            }
            //减少库存 增加销量
            $res = $goods_model->setOrDelStock($v['service_id'],$v['num']);
            if(!empty($res['code'])){
                return $res;
            }
        }
        //删除购物车
        $res = $car_model->where(['user_id'=>$user_id,'coach_id'=>$cap_id,'status'=>1])->delete();
        return true;
    }
}